package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.lab.dao.MajorDao;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.po.Major;
import com.lab.po.PageBean;

/**
 * Servlet implementation class MajorAjax
 */
@WebServlet("/majorAjax")
public class MajorAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MajorAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		System.out.println(request.getParameter("currentPage"));
		System.out.println(request.getParameter("col_id"));
		int col_id=Integer.parseInt(request.getParameter("col_id"));
		MajorDao majordao=new MajorDaoImpl();
		PageBean pagebean=new PageBean();
		Map map =new HashMap();
		map.put("col_id", col_id);
		map.put("key1", (currentPage-1)*pagebean.getCountPage());
		map.put("key2", pagebean.getCountPage());
		List<Major> majorList=majordao.queryMajor(map);
		JSONArray jsonArray=JSONArray.fromObject(majorList);
		System.out.println(jsonArray.toString());
		response.getWriter().write(jsonArray.toString());
	}

}
